package com.rbw.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rb.util.AppTextUtil;
import com.rb.util.StringUtil;
import com.rbw.cache.dao.UserCacheDao;
import com.rbw.controller.req.TechParam;
import com.rbw.controller.res.ListResult;
import com.rbw.model.IndustryBean;
import com.rbw.model.OriginBean;
import com.rbw.model.TechBean;
import com.rbw.model.TechCategoryBean;
import com.rbw.model.TradeTypeBean;
import com.rbw.persist.dao.IndustryDao;
import com.rbw.persist.dao.OriginDao;
import com.rbw.persist.dao.TechCategoryDao;
import com.rbw.persist.dao.TechDao;
import com.rbw.persist.dao.TradeTypeDao;
import com.rbw.persist.dao.UserDao;
import com.rbw.service.TechService;

@Service
public class TechServiceImpl implements TechService{
	

	@Autowired
	TechDao techImpl;

	@Autowired
	OriginDao originImpl;
	
	@Autowired
	TechCategoryDao techCategoryImpl;
	
	@Autowired
	IndustryDao industryImpl;

	@Autowired
	TradeTypeDao tradeTypeImpl;
	
	@Autowired
	UserDao userImpl;

	@Autowired
	UserCacheDao userCacheImpl;

	@Override
	public ListResult<TechBean> getLists(String sessionId, Integer page, Integer pageSize, List<String> origins, List<String> industries, 
			List<String> categories, List<String> tradetypes, String sort, String desc, String content) {
		ListResult<TechBean> result = new ListResult<>();
		Map<String, Object> params = new HashMap<>();
		params.put("origins", origins);
		params.put("industries", industries);
		params.put("categories", categories);
		params.put("tradetypes", tradetypes);
		params.put("sort", sort);
		params.put("name", content);
		int start = (page - 1) * pageSize;
		params.put("desc", desc);
		params.put("start", start);
		params.put("pageSize", pageSize);
		String uid = userCacheImpl.getUidBySession(sessionId);
		if(uid != null) {
			params.put("userId", uid);
		}
		int totalSize = techImpl.selectTotal(params);
		
		List<TechBean> beans = techImpl.selectByPage(params);
			

		result.setData(beans);
		result.setTotalSize(totalSize);
		return result;
	}

	@Override
	public TechBean getDetail(String tid) {
		TechBean t = techImpl.getDetail(tid);
		if(t != null) {
			// 阅读次数+1
			t.setCount(t.getCount() + 1);
			techImpl.update(t);
		}
		
		return t;
	}

	@Override
	public List<OriginBean> getOrigins() {
		return originImpl.getList();
	}

	@Override
	public List<TechCategoryBean> getCategories() {
		return techCategoryImpl.getList();
	}

	@Override
	public List<IndustryBean> getIndustries() {
		return industryImpl.getList();
	}

	@Override
	public List<TradeTypeBean> getTradeType() {
		return tradeTypeImpl.getList();
	}

	@Override
	public ListResult<TechBean> getUserTechs(String sessionId, int page, int pageSize) {
		ListResult<TechBean> result = new ListResult<>();
		String uid = userCacheImpl.getUidBySession(sessionId);
		Map<String, Object> params = new HashMap<>();
		int start = (page - 1) * pageSize;
		params.put("start", start);
		params.put("pageSize", pageSize);
		params.put("userId", uid);
		
		int totalSize = techImpl.selectUserTechsTotal(params);
		
		List<TechBean> beans = techImpl.selectUserTechsByPage(params);
		result.setData(beans);
		result.setTotalSize(totalSize);
		return result;
	}

	@Override
	public boolean saveTech(String sessionId, TechParam param) {
		String uid = userCacheImpl.getUidBySession(sessionId);
		boolean flag = false;
		TechBean tech = new TechBean();
		tech.setName(param.getName());
		tech.setAddress(param.getAddress());
		tech.setCategoryId(param.getCategoryId());
		tech.setConnectUser(param.getConnectUser());
		tech.setContent(param.getContent());
		tech.setEmail(param.getEmail());
		tech.setIndustryId(param.getIndustryId());
		tech.setOriginId(param.getOriginId());
		tech.setOwner(param.getOwner());
		tech.setPrice(param.getPrice());
		tech.setTelephone(param.getTelephone());
		tech.setTradeTypeId(param.getTradeTypeId());
		tech.setUserId(uid);
		String tid = param.getTid();
		if(StringUtil.checkEmpty(tid)){
			tech.setTid(AppTextUtil.getPrimaryKey());
			flag = techImpl.insert(tech) > 0;
		} else {
			tech.setTid(tid);
			flag = techImpl.update(tech) > 0;
		}
		return flag;
	}

}
